﻿// -----------------------------------------------------------------------
// <copyright company="www.3WayWebdesign.de">
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
// 
// <author>Michael Morbach</author>
// <email>Michael@3waywebdesign.de</email>
// <web>http://www.3waywebdesign.de</web>
// ----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using Microsoft.Windows.Shell;
using Tww.Wpf.Controls.Controls.Flyouts;
using Tww.Wpf.Controls.Controls.Shared.Enums;
using Tww.Wpf.Controls.Controls.Shared.Helper;

namespace Tww.Wpf.Controls.Controls.Windows
{
    /// <summary>
    /// This class eats little children.
    /// </summary>
    internal static class MetroWindowHelpers
    {
        #region  - Methoden (öffentlich) -

        /// <summary>
        /// Sets the IsHitTestVisibleInChromeProperty to a MetroWindow template child
        /// </summary>
        /// <param name="window">The MetroWindow</param>
        /// <param name="name">The name of the template child</param>
        public static void SetIsHitTestVisibleInChromeProperty<T>(this MetroWindow window, string name)
            where T : DependencyObject
        {
            if (window == null)
            {
                return;
            }
            var elementPart = window.GetPart<T>(name);
            if (elementPart != null)
            {
                elementPart.SetValue(WindowChrome.IsHitTestVisibleInChromeProperty, true);
            }
        }


        /// <summary>
        /// Handles the window commands for flyouts.
        /// </summary>
        /// <param name="window">The window.</param>
        /// <param name="flyouts">The flyouts.</param>
        /// <param name="resetBrush">The reset brush.</param>
        public static void HandleWindowCommandsForFlyouts(this MetroWindow window, IEnumerable<Flyout> flyouts,
            Brush resetBrush = null)
        {
            List<Flyout> allVisibleFlyouts =
                flyouts.Where(x => x.ElementSize != ElementSize.Collapsed && x.Position != Position.Bottom)
                    .OrderByDescending(Panel.GetZIndex)
                    .ToList();

            if (!allVisibleFlyouts.Any())
            {
                if (resetBrush == null)
                {
                    window.ResetAllWindowCommandsBrush();
                }
                else
                {
                    window.ChangeAllWindowCommandsBrush(resetBrush);
                }
            }

            Flyout topFlyout = allVisibleFlyouts.FirstOrDefault(x => x.Position == Position.Top);
            if (topFlyout != null)
            {
                window.UpdateWindowCommandsForFlyout(topFlyout);
            }
            else
            {
                Flyout leftFlyout = allVisibleFlyouts.FirstOrDefault(x => x.Position == Position.Left);
                if (leftFlyout != null)
                {
                    window.UpdateWindowCommandsForFlyout(leftFlyout);
                }
                Flyout rightFlyout = allVisibleFlyouts.FirstOrDefault(x => x.Position == Position.Right);
                if (rightFlyout != null)
                {
                    window.UpdateWindowCommandsForFlyout(rightFlyout);
                }
            }
        }

        public static void ResetAllWindowCommandsBrush(this MetroWindow window)
        {
            if (window.OverrideDefaultWindowCommandsBrush == null)
            {
                window.InvokeCommandButtons(x => x.ClearValue(Control.ForegroundProperty));
                window.WindowButtonCommands.ClearValue(Control.ForegroundProperty);
            }
            else
            {
                window.ChangeAllWindowCommandsBrush(window.OverrideDefaultWindowCommandsBrush);
            }
        }

        public static void UpdateWindowCommandsForFlyout(this MetroWindow window, Flyout flyout)
        {
            Brush brush = flyout.Foreground;

            window.ChangeAllWindowCommandsBrush(brush, flyout.Position);
        }

        public static void ChangeWindowCommandButtonsBrush(this MetroWindow window, string brush)
        {
            window.InvokeCommandButtons(x => x.SetResourceReference(Control.ForegroundProperty, brush));
        }

        public static void ChangeWindowCommandButtonsBrush(this MetroWindow window, string brush, Position position)
        {
            window.InvokeCommandButtons(x => x.SetResourceReference(Control.ForegroundProperty, brush), position);
        }

        #endregion

        #region  - Methoden (privat) -

        private static void ChangeWindowCommandButtonsBrush(this MetroWindow window, Brush brush)
        {
            window.InvokeCommandButtons(x => x.SetValue(Control.ForegroundProperty, brush));
        }

        private static void ChangeWindowCommandButtonsBrush(this MetroWindow window, Brush brush, Position position)
        {
            window.InvokeCommandButtons(x => x.SetValue(Control.ForegroundProperty, brush), position);
        }

        private static void InvokeCommandButtons(this MetroWindow window, Action<ButtonBase> action)
        {
            if (window.RightWindowCommandsPresenter == null || window.LeftWindowCommandsPresenter == null)
            {
                return;
            }

            IEnumerable<ButtonBase> allCommandButtons = ((WindowCommands) window.RightWindowCommandsPresenter.Content)
                .FindChildren<ButtonBase>()
                .Concat(((WindowCommands) window.LeftWindowCommandsPresenter.Content).FindChildren<ButtonBase>());
            foreach (ButtonBase b in allCommandButtons)
            {
                action(b);
            }
        }

        private static void InvokeCommandButtons(this MetroWindow window, Action<ButtonBase> action, Position position)
        {
            if (window.RightWindowCommandsPresenter == null || window.LeftWindowCommandsPresenter == null)
            {
                return;
            }

            IEnumerable<ButtonBase> allCommandButtons = Enumerable.Empty<ButtonBase>();
            if (position == Position.Right || position == Position.Top)
            {
                allCommandButtons =
                    allCommandButtons.Concat(
                        ((WindowCommands) window.RightWindowCommandsPresenter.Content).FindChildren<ButtonBase>());
            }
            if (position == Position.Left || position == Position.Top)
            {
                allCommandButtons =
                    allCommandButtons.Concat(
                        ((WindowCommands) window.LeftWindowCommandsPresenter.Content).FindChildren<ButtonBase>());
            }
            foreach (ButtonBase b in allCommandButtons)
            {
                action(b);
            }
        }

        private static void ChangeAllWindowCommandsBrush(this MetroWindow window, Brush brush)
        {
            window.ChangeWindowCommandButtonsBrush(brush);
            window.WindowButtonCommands.SetValue(Control.ForegroundProperty, brush);
        }

        private static void ChangeAllWindowCommandsBrush(this MetroWindow window, Brush brush, Position position)
        {
            window.ChangeWindowCommandButtonsBrush(brush, position);
            if (position == Position.Right || position == Position.Top)
            {
                window.WindowButtonCommands.SetValue(Control.ForegroundProperty, brush);
            }
        }

        #endregion
    }
}